package com.authine.cloudpivot.ext.dto.model.settle;


import com.authine.cloudpivot.ext.Enum.ContractPaymentType;
import com.authine.cloudpivot.ext.Enum.SettleApplyType;
import com.authine.cloudpivot.ext.Enum.SettleBusinessType;
import com.authine.cloudpivot.ext.Enum.SettleType;
import com.authine.cloudpivot.ext.dto.form.InvoiceApplyForm;
import com.authine.cloudpivot.ext.dto.model.invoice.InvoiceApply;
import com.authine.mvp.app.launcher.domain.AttachmentFormat;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 结算申请(基于已开发票)
 *
 * @author 张天赐
 * 2020/3/4
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(description = "结算申请")
public class SettleInvoice {
    /**
     * 结算单ID
     */
    @Schema(description = "结算单ID")
    private String id;
    /**
     * 目标ID
     */
    @Schema(description = "目标ID")
    private String reference_id;
    /**
     * ID类型枚举
     */
    @Schema(description = "结算单申请维度")
    private SettleApplyType apply_type;
    /**
     * 结算业务类型
     */
    @Schema(description = "结算业务类型,销售收款新增默认为SaleReceivable")
    private SettleBusinessType business_type;
    /**
     * 支付类型
     */
    @Schema(description = "支付类型")
    private SettleType type;
    /**
     * 当前操作者角色
     */
//    @Schema(description = "角色")
//    private SettleRoleEnum roleEnum;
    /**
     * 收款金额
     */
    @Schema(description = "收款金额")
    private BigDecimal total;
    /**
     * 当前操作申请人用户ID
     */
    @Schema(description = "当前操作申请人用户ID", hidden = true)
    private String apply_member_id;
    /**
     * 当前操作申请人用户全称
     */
    @Schema(description = "当前操作申请人用户全称", hidden = true)
    private String apply_member_name;
    /**
     * 确认人ID
     */
    @Schema(description = "确认人ID", hidden = true)
    private String confirm_member_id;
    /**
     * 确认人全称
     */
    @Schema(description = "确认人全称")
    private String confirm_member_name;
    /**
     * 销售开户行
     */
    @Schema(description = "销售开户行")
    private String sale_bank_info;
    /**
     * 销售收款银行账号
     */
    @Schema(description = "收款银行账号")
    private String sale_bank_no;
    /**
     * 销售收款银行账户名
     */
    @Schema(description = "收款银行账户名")
    private String sale_bank_account;
    /**
     * 销售收款电话
     */
    @Schema(description = "销售收款电话")
    private String sale_phone;
    /**
     * 销售收款备注
     */
    @Schema(description = "收款备注")
    private String sale_memo;
    /**
     * 销售收款组织ID
     */
    @Schema(description = "收款组织ID")
    private SelectorFormat sale_organization_id;
    /**
     * 销售收款组织名称
     */
    @Schema(description = "收款组织名称")
    private String sale_organization_name;
    /**
     * 采购付款组织ID
     */
    @Schema(description = "付款组织ID")
    private SelectorFormat purchase_organization_id;
    /**
     * 采购付款组织名称
     */
    @Schema(description = "付款组织名称")
    private String purchase_organization_name;
    /**
     * 采购付款组织抬头
     */
    @Schema(description = "付款组织抬头")
    private String purchase_name;
    /**
     * 收款时间
     */
    @Schema(description = "收款时间")
    private Date confirm_time;
    /**
     * 是否提交
     */
    @Schema(description = "是否提交")
    private Boolean submit;
    /**
     * 自定义垫资服务费结算比例（平台角色使用）
     */
    @Schema(description = "自定义垫资服务费结算比例（平台角色使用）")
    private BigDecimal interest_rate;
    /**
     * 自定义平台服务费结算比例（平台角色使用）
     */
    @Schema(description = "自定义平台服务费结算比例（平台角色使用）")
    private BigDecimal platform_rate;
    /**
     * 垫资服务费
     */
    @Schema(description = "垫资服务费")
    private BigDecimal interest;

    /**
     * 平台服务费
     */
    @Schema(description = "平台服务费")
    private BigDecimal platform;

    /**
     * 结算申请明细（例：发票单）
     */
    @Schema(description = "结算申请明细")
    private List<SettleInvoiceDetail> settleInvoiceDetails;

    @Schema(description = "附件")
    private List<AttachmentFormat> bank_serial_image;

    /**
     * 流水号
     */
    @Schema(description = "流水号")
    private String bank_serial_number;

    /**
     * 是否代购
     */
    @Schema(description = "是否代购")
    private boolean contract;

    /**
     * 代购费用类型
     */
    @Schema(description = "代购费用类型,Goods=货款，ServiceFee=服务费")
    private ContractPaymentType contract_payment_type;

    /**
     * 服务费发票信息
     */
    @Schema(description = "服务费发票信息")
    private InvoiceApplyForm invoiceServiceApplyProcess;

    /**
     * 申请服务费发票时返回信息（当获取多个采购发票信息，需要提示供应商，手动维护服务费发票信息）
     */
    @Schema(description = "服务费发票信息")
    private String invoiceServiceApplyProcessMsg;

    public String getNumber() {
        if (null == settleInvoiceDetails || settleInvoiceDetails.isEmpty()) {
            return "";
        }
        return StringUtils.join(settleInvoiceDetails.stream().map(SettleInvoiceDetail::getNumber).collect(Collectors.toSet()), ",");
    }

    public List<String> getDetailApplyIds() {
        if (null == settleInvoiceDetails || settleInvoiceDetails.isEmpty()) {
            return null;
        }
        return settleInvoiceDetails.stream().map(SettleInvoiceDetail::getApply_id).distinct().collect(Collectors.toList());
    }


}
